package com.itheima.controller.user;

import com.itheima.common.constant.RedisMessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.dto.MemberLoginDTO;
import com.itheima.service.UserMemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * 会员接口
 */
@RestController("userMemberController")
@RequestMapping("/member")
@Slf4j
public class MemberController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserMemberService userMemberService;
    /**
     * 会员登录
     * @param memberLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody MemberLoginDTO memberLoginDTO) {
        String telephone =memberLoginDTO.getTelephone();
        //取得redis验证码
        String redisValidateCode = (String) redisTemplate.opsForValue().get(telephone + RedisMessageConstant.SENDTYPE_LOGIN);
        if (redisValidateCode != null) {
            if (redisValidateCode.equals(memberLoginDTO.getValidateCode())) {
               userMemberService.login(memberLoginDTO);
            } else {
                throw  new RuntimeException("请输入正确的验证码");
            }
        }else {
            throw  new RuntimeException("验证码以失效");
        }
        return Result.error("登录成功");
    }
}
